package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.vo.ContentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.entity.ContentEntity;
import io.renren.modules.sys.service.ContentService;
import io.renren.common.utils.Pages;
import io.renren.common.utils.ResponseModel;


/**
 *
 *
 * @author xuewenjun
 * @email xwj@kafukeji.cn
 * @date 2021-07-30 14:39:09
 */
@RestController
@RequestMapping("/sys/content")
@Api(tags = "后台管理-内容管理")
public class ContentController extends AbstractController{
    @Autowired
    private ContentService contentService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResponseModel<Pages<ContentEntity>> list(@RequestBody ContentVo content){
        Long userId = super.getUserId();
        if(userId != 1) {
            content.setCreateId(userId);
        }
        Pages<ContentEntity> page = contentService.queryPage(content);

        return ResponseModel.of(page);
    }

    @PostMapping("/listAll")
    @ApiOperation("列表-全")
    public ResponseModel<List<ContentEntity>> listAll(@RequestBody ContentVo content) {
        List<ContentEntity> list = contentService.listAll(content);
        return ResponseModel.of(list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public ResponseModel<ContentEntity> info(@PathVariable("id") Integer id){
        ContentEntity content = contentService.getById(id);

        return ResponseModel.of(content);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("新增")
    public ResponseModel save(@RequestBody ContentEntity content){
        SysUserEntity user = getUser();
        content.setCreateId(user.getUserId());
        content.setCreateName(user.getUsername());
        content.setCreateTime(new Date());
        contentService.save(content);

        return ResponseModel.of(content);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseModel update(@RequestBody ContentEntity content){
        SysUserEntity user = getUser();
        content.setUpdateId(user.getUserId());
        content.setUpdateName(user.getUsername());
        contentService.updateById(content);

        return ResponseModel.of(content);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除或批量删除")
    public ResponseModel delete(@RequestBody Integer[] ids){
        contentService.removeByIds(Arrays.asList(ids));

        return ResponseModel.ok();
    }


    @GetMapping("/detail/{type}")
    @ApiOperation("详情")
    public ResponseModel<ContentEntity> info(@PathVariable("type") String type){
        ContentEntity rs = contentService.getByType(type);

        return ResponseModel.of(rs);
    }

    @PostMapping("/one")
    @ApiOperation("详情过滤")
    public ResponseModel<ContentEntity> info(@RequestBody ContentEntity content){
        ContentEntity rs = contentService.getInfo(content);

        return ResponseModel.of(rs);
    }

}
